package model;

import java.awt.EventQueue;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.SQLException;

import views.GTCarRental;

public class Model {
	private static GTCarRental frame;
	private static String currentUser;
	public static String currentUserType;
	private static Connection con;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		ConnectionManager cm = new ConnectionManager();
		con = cm.getNewConnection();
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new GTCarRental();
					Toolkit tk = Toolkit.getDefaultToolkit();  
					int xSize = ((int) tk.getScreenSize().getWidth());  
					int ySize = ((int) tk.getScreenSize().getHeight());  
					frame.setSize(xSize,ySize);  
					if (currentUser == null)
					{
						frame.disableTabs();
					}
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
				
	}
	
	public static Connection getConnection() {
		try {
			if(con.isClosed()) {
				createConnection();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}
	
	private static Connection createConnection() {
		ConnectionManager cm = new ConnectionManager();
		con = cm.getNewConnection();
		return con;
	}
	
	
	/**
	 * @return the currentUser
	 */
	public static String getCurrentUser() {
		return currentUser;
	}
	/**
	 * @param currentUser the currentUser to set
	 */
	public static void setCurrentUser(String c) {
		currentUser = c;
		if (c != null)
		{
			frame.populate();
		}	
	}
	/**
	 * @return the currentUserType
	 */
	public static String getCurrentUserType() {
		return currentUserType;
	}
	/**
	 * @param currentUserType the currentUserType to set
	 */
	public static void setCurrentUserType(String currentUserType) {
		Model.currentUserType = currentUserType;
		if (currentUserType == null)
		{
			frame.setUser(null);
			return;
		}
		else
		{
			frame.populate();
			if (currentUserType.equals("MEMBER"))
			{
				frame.setUser("MEMBER");
			}
			else if (currentUserType.equals("ADMINISTRATOR"))
			{
				frame.setUser("ADMINISTRATOR");
			}
			else if (currentUserType.equals("EMPLOYEE"))
			{
				frame.setUser("EMPLOYEE");
			}
		}
		return;
	}
	
	
	
}
